package edu.gatech.cs2340.sharks.fms;

/**
 * AddTagActivity.java
 * @author Miri Baker
 * 
 * Allows an admin user to enter a name and description
 * for a new tag, adding it to the database for the client
 * to select.
 */

import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class AddTagActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_add_tag);
	}

	/**
	 * Will add a tag that has been typed into the EditTextView 
	 * to the list of valid tags.  Called on button press.
	 * 
	 * @param view
	 */
	public void addNewTag(View view)
	{
		// Gets views in the layout to be programatically updated.
		TextView errorText = (TextView)findViewById(R.id.errorText);
		String newTagName = ((EditText)findViewById(R.id.enterTagName)).getText().toString();
		String newTagDesc = ((EditText)findViewById(R.id.enterTagDesc)).getText().toString();
		
		// Checks input in the EditText fields.
		if(newTagName==null || newTagDesc==null) Info.alert(this, "Name or description is null.");
		else if(newTagName.equals("") || newTagDesc.equals("")) errorText.setText("Please enter a name and description.");
		else
		{
			Status tagCreated = DBMgr.createTag(DBMgr.user, newTagName, newTagDesc);
			if(tagCreated.code().equals(Status.Code.OK))
			{
				Intent backToTags = new Intent(this, ManageTagsActivity.class);
				startActivity(backToTags);
			}
			else errorText.setText(tagCreated.toString());
		}

	}
}
